/**
 * Created by bianlian on 16/9/19.
 */

import React, { Component, PropTypes } from 'react';
import {
    AppRegistry,
    StyleSheet,
    View,
    ListView,
    Text,
    Image,
    TouchableOpacity,
    BackAndroid,
    Platform,
    Alert
} from 'react-native';
import { MtContainer, MtSeparator, MtChar, MtColor, MtActionButton, MtToast } from 'martian-ui';
const MtIcon = require('martian-icon');
const { MtT7 } = MtChar;


import YaguanRoomDevices from '../container/YaguanRoomDevices';
import {fetchHouseList, fetchRoomList} from '../action/girlAction';
import Dimensions from 'Dimensions';
const width = Dimensions.get('window').width;
const height = Dimensions.get('window').height / 2;


class YaguanHomeListCell extends Component {

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {};

    }

    /**
     * 调用数据 :
     *      1. 当触发Action方法请求数据反馈到Reducer中
     *      2. Reducer通过ActionType判断该更新哪个UI中的State
     *      3. 最后刷新界面显示数据
     */
    componentWillMount() {


        if (Platform.OS === 'android') {
            BackAndroid.addEventListener('hardwareBackPress', () => this.onBackAndroid());
        }
    }

    componentDidMount() {
        if (Platform.OS === 'android') {
            BackAndroid.addEventListener('hardwareBackPress', () => this.onBackAndroid());
        }
    }


    /**
     * 监听Android返回按键
     * @returns {boolean}
     */
    onBackAndroid() {

        const {navigator} = this.props;
        if (navigator.getCurrentRoutes().length > 1) {
            navigator.pop();
            return true;
        }
        //退出
        return false;

    }



    render() {
        const { room, navigator, isFetching } = this.props;
        const { name, devices } = room || {};

        const Button = (
            <View style={styles.buttonContainer}>
                <MtActionButton
                    containerStyle={{ height: 26, width: 75, marginLeft: 20 }}
                    foreground="light"
                    onPress={() => {
                        Alert.alert(
                            null,
                            '您是否进入房间',
                            [
                                { text: '否' },
                                {
                                    text: '是 ',
                                    onPress: () => {
                                        navigator.push({
                                            component: YaguanRoomDevices,
                                            params: {
                                                devices,
                                                name,
                                                isFetching
                                            }
                                        })
                                    },
                                },
                            ]
                        );
                    }}
                >
                    进入房间
                </MtActionButton>
            </View>
        );
        return (
            <View style={styles.rowContainer}>
                <View style={styles.favItem}>
                    <MtIcon
                        name="fav-solid"
                        size={30}
                        color={MtColor.main}
                    />
                    <MtT7>
                        {name}
                    </MtT7>
                    {Button}
                </View>
            </View>
        );
    }



}

const styles = StyleSheet.create({
    rowContainer: {
        backgroundColor: 'white',
        marginTop: 10,
    },
    tagInner: {
        alignItems: 'flex-end',
        height: 55,
        justifyContent: 'center',
    },
    tagColorBox: {
        height: 26,
        paddingHorizontal: 10,
        justifyContent: 'center',
        alignItems: 'center',
    },
    separator: {
        paddingHorizontal: 15,
    },
    favItem: {
        height: 50,
        paddingHorizontal: 15,
        flexDirection: 'row',
        alignItems: 'center',
    },
    circular: {
        borderRadius: 2,
        height: 26,
        backgroundColor: '#e3f1fe',
        alignItems: 'center',
        justifyContent: 'center',
    },
    buttonContainer: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'flex-end',
    },
});

module.exports = YaguanHomeListCell;